package org.monkey.common.generator.entity;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import org.monkey.common.constant.entity.DbConfig;
import org.monkey.common.constant.util.MonkeyStrUtil;

@Data
@ApiModel
public class GeneratorConfig {

  @ApiModelProperty(name = "数据源配置", value = "数据源配置")
  private DbConfig dbConfig;
  @ApiModelProperty(name = "entityInfos", value = "表和实体")
  private List<EntityConfig> entityConfigs;
  @ApiModelProperty(name = "tablePre", value = "表名前缀")
  private String tablePre;
  @ApiModelProperty(name = "hump", value = "是否转驼峰", example = "true")
  private boolean hump;
  @ApiModelProperty(name = "packageName", value = "包名")
  private String packageName;
  @ApiModelProperty(name = "moduleName", value = "模块名称:英文")
  private String moduleName;
  @NotBlank
  @ApiModelProperty(name = "moduleDescription", value = "模块描述", example = "true")
  private String moduleDescription;
  @NotBlank
  @ApiModelProperty(name = "parentModuleName", value = "父模块名称", example = "monkey-gospel")
  private String parentModuleName;
  @ApiModelProperty(name = "applicationName", value = "启动类名称")
  private String applicationName;
  @ApiModelProperty(name = "serviceName", value = "服务名：英文")
  private String serviceName;
  @ApiModelProperty(name = "serviceDescription", value = "服务描述")
  private String serviceDescription;
  @ApiModelProperty(name = "properties", value = "其它生成参数")
  private Map<String, String> properties;

  @ApiModelProperty(
      name = "templates",
      example = "['active','add','app','boot','controller','convert','dao','docket','dto','impl','list','log','mapper','page','po','pom','save','service','upd','vo']")
  private List<String> templates;

  @JsonIgnore
  @JSONField(serialize = false)
  @com.alibaba.fastjson2.annotation.JSONField(serialize = false)
  private Map<String, Object> cacheMap;

  public void setModuleName(String moduleName) {
    if(StrUtil.isNotBlank(moduleName)) {
      moduleName = MonkeyStrUtil.lowerFirstCase(moduleName);
      moduleName = MonkeyStrUtil.humpToUnderline(moduleName);
      moduleName = moduleName.replace("_", "-");
      this.moduleName = moduleName;
    }
  }

  public synchronized Map<String, Object> toParamMap(JavaEntityInfo entry) {
    if (cacheMap == null) {
      Map<String, Object> map = new HashMap<>();
      map.put("packageName", packageName);
      map.put("moduleName", moduleName);
      map.put("moduleDescription", moduleDescription);
      map.put("parentModuleName", parentModuleName);
      map.put("applicationName", applicationName);
      map.put("serviceName", serviceName);
      map.put("serviceDescription", serviceDescription);
      map.put("entry", entry);
      if (properties != null) {
        map.putAll(properties);
      }
      return map;
    } else {
      return cacheMap;
    }
  }
}
